<?php
/**
 * Created by PhpStorm.
 * User: luo
 * Date: 2018/10/29
 * Time: 22:16
 */

namespace app\models\traits;

use Yii;
use yii\helpers\Json;
use yii\helpers\StringHelper;

trait Attach
{

    /**
     * 仅显示单张图片
     * @return string
     */
    public function showSimpleCover($is_empty = 0){
        $cover = '';
        if ($this->type == 5) {
            $attach = Json::decode($this->video_attach);
            $cover = $attach[0]['cover'];
        } else {
            if($this->attach){
                $attach = Json::decode($this->attach);
                if (count($attach)) {
                    $cover = $attach[0]['name'];
                    if($cover && !StringHelper::startsWith($cover, 'http', false)){
                        $cover = $this->show($cover);
                    }
                }
            }
        }
        
        if (!$cover && $is_empty) {
            return "";
        }
        if(!$cover){
            $cover = Yii::$app->params['logo'];
        }
        //var_dump($cover);die;
        return $cover;
    }

    /**
     * 给前台用户展现简单版的附件信息
     * @return array|mixed
     */
    public function showSimpleAttach(){
        if($this->attach){
            $attach = Json::decode($this->attach);
            $new_attach = [];
            foreach ($attach as $k=>$v){
                if(isset($v['show']) && $v['show'] == 0){
                    continue;
                }
                $new_attach[$k] = $v;
                if(!StringHelper::startsWith($v['name'], 'http', false)){
                    $new_attach[$k]['name'] = $this->showWithWaterMark($v['name'], $this->showCreateNickname(), $this->use_water);
                }
                if(!isset($v['type'])){
                    $new_attach[$k]['type'] = self::ATTACH_IMG;
                }
                if(!isset($v['desc'])){
                    $new_attach[$k]['desc'] = '';
                }
                if(!isset($v['width'])){
                    $new_attach[$k]['width'] = 0;
                }
                if(!isset($v['height'])){
                    $new_attach[$k]['height'] = 0;
                }
                $path_type = pathinfo($v['name'], PATHINFO_EXTENSION);
                $video_arr = ["avi","wmv","mpeg","mp4","mov","mkv","flv","f4v","m4v","rmvb","rm","3gp","dat","ts","mts","vob"];
                $new_attach[$k]['is_video'] = in_array($path_type, $video_arr) ? 1 : 0;
            }
            return array_values($new_attach);
        }else{
            return [];
        }
    }

    /**
     * 给后台用户展现完整版的附件信息
     * @return array|mixed
     */
    public function showAttachAll(){
        if($this->attach_all){
            $attach = Json::decode($this->attach_all);
            foreach ($attach as $k=>$v){
                if(!isset($v['show'])){
                    $attach[$k]['show'] = 1;
                }else{
                    $attach[$k]['show'] = (int)$v['show'];
                }
                if(!StringHelper::startsWith($v['name'], 'http', false)){
                    $attach[$k]['name'] =  $this->show($v['name']);
                }
                // if(!isset($v['type'])){
                //     $attach[$k]['type'] = self::ATTACH_IMG;
                // }
                // if(!isset($v['desc'])){
                //     $attach[$k]['desc'] = '';
                // }
                // if(!isset($v['width'])){
                //     $attach[$k]['width'] = 0;
                // }
                // if(!isset($v['height'])){
                //     $attach[$k]['height'] = 0;
                // }
            }
            return $attach;
        }else{
            return [];
        }
    }

    /**
     * 给后台用户展现完整版的附件信息
     * @return array|mixed
     */
    public function showAttach(){
        if($this->attach){
            $attach = Json::decode($this->attach);
            foreach ($attach as $k=>$v){
                if(!isset($v['show'])){
                    $attach[$k]['show'] = 1;
                }else{
                    $attach[$k]['show'] = (int)$v['show'];
                }
                if(!StringHelper::startsWith($v['name'], 'http', false)){
                    $attach[$k]['name'] =  $this->show($v['name']);
                }
                if(!isset($v['type'])){
                    $attach[$k]['type'] = self::ATTACH_IMG;
                }
                if(!isset($v['desc'])){
                    $attach[$k]['desc'] = '';
                }
                if(!isset($v['width'])){
                    $attach[$k]['width'] = 0;
                }
                if(!isset($v['height'])){
                    $attach[$k]['height'] = 0;
                }
            }
            return $attach;
        }else{
            return [];
        }
    }

    public function onDoAllAttach(){
        if($this->attach_all === null || !$this->attach_all){
            $this->attach_all = [];
        }
        if(!is_array($this->attach_all)){
            $this->attach_all = Json::decode($this->attach_all);
        }
        $qiniu = Yii::$app->params['qiniu'];
        $attach_all = [];
        if ($this->attach_all) {
            foreach ($this->attach_all as $k=>$v){
                $attach_all[$k] = $v;
                $attach_all[$k]['name'] = str_replace($qiniu['url'], '', $v['name']);
            }
        }
        $this->attach_all = Json::encode($attach_all);
    }

    public function onDoArticleAttach(){
        if($this->attach === null || !$this->attach){
            $this->attach = [];
        }
        if(!is_array($this->attach)){
            $this->attach = Json::decode($this->attach);
        }
        $qiniu = Yii::$app->params['qiniu'];
        $attach = [];
        $index = 10000000000;
        if ($this->type == 1) {
            $index = $this->cover_type == 1 ? 1 : 3;
        }
        if ($this->attach) {
            foreach ($this->attach as $k=>$v){
                if ($k < $index) {
                    $attach[$k] = $v;
                    $attach[$k]['name'] = str_replace($qiniu['url'], '', $v['name']);
                }
            }
        }
        $this->attach = Json::encode($attach);
    }

    public function onDoVideoAttach(){
        if($this->video_attach === null || !$this->video_attach){
            $this->video_attach = [];
        }
        if(!is_array($this->video_attach)){
            $this->video_attach = Json::decode($this->video_attach);
        }
        $qiniu = Yii::$app->params['qiniu'];
        $video_attach = [];
        if ($this->video_attach) {
            foreach ($this->video_attach as $k=>$v){
                $video_attach[$k] = $v;
                //$video_attach[$k]['name'] = str_replace($qiniu['url'], '', $v['name']);
            }
        }
        $this->video_attach = Json::encode($video_attach);
    }

    public function onDoAttach(){
        if($this->attach === null || !$this->attach){
            $this->attach = [];
        }
        if(!is_array($this->attach)){
            $this->attach = Json::decode($this->attach);
        }
        $qiniu = Yii::$app->params['qiniu'];
        $attach = [];
        if ($this->attach) {
            foreach ($this->attach as $k=>$v){
                $attach[$k] = $v;
                $attach[$k]['name'] = str_replace($qiniu['url'], '', $v['name']);
            }
        }
        $this->attach = Json::encode($attach);
    }
}